package com.xinchuang.mapper.targetSystem.config;

import com.xinchuang.entity.targetSystem.config.SystemConfig;
import org.apache.ibatis.annotations.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName SystemConfigMapper
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2022-08-09 10:01
 * @Verion V1.0
 */
public interface SystemConfigMapper {

    @Insert(value = "INSERT INTO system_config (id,name,remark,color,type) VALUES (#{id},#{name},#{remark},#{color},#{type})")
    int addSystemConfig(SystemConfig systemConfig);

    @Select(value = {
            "SELECT id,name,remark,color,type FROM system_config WHERE name = #{name}"
    })
    SystemConfig selectSystemConfigOnly(String name);

    @Select(value = {
            "SELECT id,name,remark,color,type FROM system_config WHERE id = #{id}"
    })
    SystemConfig selectSystemConfigIdOnly(String id);

    @Delete(value = "DELETE FROM system_config WHERE id = #{id}")
    int deleteSystemConfig(String id);

    @Update(value = {
            "<script>",
            "UPDATE system_config ",
            "<set>",
            "<if test = 'name != null'>name = #{name},</if>",
            "remark = #{remark},",
            "<if test = 'color != null'>color = #{color},</if>",
            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int updateSystemConfig(SystemConfig systemConfig);

    @Select(value = {
            "SELECT id AS value,name AS label FROM system_config WHERE type = 1"
    })
    List<Map<String, Object>> selectSystemConfigPull();

    @Select(value = {
            "SELECT id,name,remark,color,type FROM system_config WHERE type = 1"
    })
    List<SystemConfig> selectSystemConfigList();

    @Select(value = {"SELECT COUNT(1) FROM system_config"})
    Integer selectSystemConfigAll();

    @Select(value = {
            "SELECT relation_type FROM systems_relationship GROUP BY relation_type"
    })
    List<String> selectSystemRelationTypeList();

    @Update(value = {
            "UPDATE systems_relationship SET relation_type = #{newType} WHERE relation_type = #{oldType};"
    })
    int updateSystemsRelationType(@Param("oldType") String oldType, @Param("newType") String newType);

    @Select(value = {
            "SELECT ",
            "COUNT(case WHEN radius > ${near} AND radius < ${middle} THEN 1 END) AS '近程',",
            "COUNT(case WHEN radius >= ${middle} AND radius < ${far} THEN 1 END) AS '中程',",
            "COUNT(case WHEN radius >= ${far} THEN 1 END) AS '远程' ",
            "FROM type_else ",
            "WHERE ",
            "id IN ",
            "(SELECT target_id FROM target_base_data WHERE target_classify IN (SELECT type_id FROM target_type WHERE type_name LIKE '%${typeName}%') ",
            "AND delete_sign = 1)"
    })
    LinkedHashMap<String, Object> selectEffectScopeStat(Integer near, Integer middle, Integer far, String typeName);

    @Select(value = {
            "SELECT ",
            "COUNT(case WHEN radius > ${near} AND radius < ${middle} THEN 1 END) AS '近程',",
            "COUNT(case WHEN radius >= ${middle} AND radius < ${far} THEN 1 END) AS '中程',",
            "COUNT(case WHEN radius >= ${far} THEN 1 END) AS '远程' ",
            "FROM type_else ",
            "WHERE ",
            "id IN ",
            "(SELECT target_id FROM target_base_data WHERE target_classify IN (SELECT type_id FROM target_type WHERE type_name LIKE '%${typeName}%') ",
            "AND delete_sign = 1 ",
            "AND target_id IN (SELECT target_id FROM system_relation WHERE s_id  = #{id})",
            ")"
    })
    LinkedHashMap<String, Object> selectEffectScopeStatSystem(String id, Integer near, Integer middle, Integer far, String typeName);

    @Select(value = {
            "SELECT ",
            "COUNT(case WHEN radius > ${near} AND radius < ${middle} THEN 1 END) AS '近程',",
            "COUNT(case WHEN radius >= ${middle} AND radius < ${far} THEN 1 END) AS '中程',",
            "COUNT(case WHEN radius >= ${far} THEN 1 END) AS '远程' ",
            "FROM type_else ",
            "WHERE ",
            "id IN ",
            "(SELECT target_id FROM target_base_data WHERE target_classify IN (SELECT type_id FROM target_type WHERE type_name LIKE '%${typeName}%') ",
            "AND delete_sign = 1 ",
            "AND target_id IN (SELECT target_id FROM systems_relation WHERE sys_id  = #{id})",
            ")"
    })
    LinkedHashMap<String, Object> selectEffectScopeStatSystems(String id, Integer near, Integer middle, Integer far, String typeName);

    @Select(value = {
            "SELECT tt.type_name AS name,COUNT(1) AS value FROM target_base_data AS td JOIN target_type AS tt ON td.target_classify = tt.type_id WHERE td.delete_sign = 1 GROUP BY td.target_classify"
    })
    List<Map<String, Object>> selectTypeStat();

    @Select(value = {
            "SELECT tt.type_name AS name,COUNT(1) AS value FROM target_base_data AS td JOIN target_type AS tt ON td.target_classify = tt.type_id WHERE td.delete_sign = 1 ",
            "AND td.target_id IN (SELECT target_id FROM system_relation WHERE s_id  = #{id}) ",
            "GROUP BY td.target_classify"
    })
    List<Map<String, Object>> selectTypeStatSystem(String id);

    @Select(value = {
            "SELECT tt.type_name AS name,COUNT(1) AS value FROM target_base_data AS td JOIN target_type AS tt ON td.target_classify = tt.type_id WHERE td.delete_sign = 1 ",
            "AND td.target_id IN (SELECT target_id FROM systems_relation WHERE sys_id  = #{id}) ",
            "GROUP BY td.target_classify"
    })
    List<Map<String, Object>> selectTypeStatSystems(String id);

    @Select(value = "SELECT COUNT(1) FROM target_base_data WHERE delete_sign = 1")
    int selectTargetTotal();

    @Select(value = "SELECT COUNT(1) FROM system_relation WHERE s_id = #{id}")
    int selectSystemTargetTotal(String id);

    @Select(value = "SELECT COUNT(1) FROM systems_relation WHERE sys_id = #{id}")
    int selectSystemsTargetTotal(String id);

}
